package cn.rui.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 生成公共资源控制类
 * @author 徽州大都督
 * @date 2021/4/19
 */
@RequestMapping("sys")
@Controller
public class CommonController {

    @RequestMapping("getVerify")
    public String LineCaptcha(HttpServletRequest request, HttpServletResponse response) {
        //生成验证码图片
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha (200, 100);
        try {
            request.getSession ().setAttribute ("CAPTCHA_KEY", lineCaptcha.getCode ());
            response.setContentType ("image/png");//告诉浏览器输出内容为图片
            response.setHeader ("Pragma", "No-cache");//禁止浏览器缓存
            response.setHeader ("Cache-Control", "no-cache");
            response.setDateHeader ("Expire", 0);
            lineCaptcha.write (response.getOutputStream ());
            String code = lineCaptcha.getCode ();
            //保存验证码
            request.getSession ().setAttribute ("index_code",code);


        } catch (IOException e) {
            e.printStackTrace ();
        }

        return null;
    }
}
